
package com.jf.cloud.supplier.controller.supplier;

import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.supplier.constant.AuditStatus;
import com.jf.cloud.supplier.dto.SupplierCompanyDTO;
import com.jf.cloud.supplier.model.SupplierCompany;
import com.jf.cloud.supplier.service.SupplierAuditingService;
import com.jf.cloud.supplier.service.SupplierCompanyService;
import com.jf.cloud.supplier.vo.SupplierCompanyVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zz
 * @Date 2021/4/25 16:31
 */
@RestController
@RequestMapping("/s/apply_supplier/supplier_company")
@Tag(name = "supplier-供应商工商信息")
public class SupplierCompanyController {

    @Autowired
    private SupplierCompanyService supplierCompanyService;

    @Autowired
    private SupplierAuditingService supplierAuditingService;
    @Autowired
    private MapperFacade mapperFacade;

    @PostMapping
    @Operation(summary = "新增供应商工商信息" , description = "新增供应商工商信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SupplierCompanyDTO supplierCompanyDTO) {
        Long supplierId = this.getSupplierId();
        SupplierCompany supplierCompany = mapperFacade.map(supplierCompanyDTO, SupplierCompany.class);
        supplierCompany.setSupplierId(supplierId);
        supplierCompany.setStatus(AuditStatus.WAITAUDIT.value());
        supplierCompanyService.save(supplierCompany, true);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "供应商更新工商信息,仅在申请开店时调用" , description = "供应商更新工商信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SupplierCompanyDTO supplierCompanyDTO) {
        SupplierCompany supplierCompany = mapperFacade.map(supplierCompanyDTO, SupplierCompany.class);
        supplierCompany.setStatus(AuditStatus.WAITAUDIT.value());
        supplierCompanyService.checkInfo(supplierCompany, false);
        supplierCompanyService.update(supplierCompany);
        return ServerResponseEntity.success();
    }

    @PostMapping("/storage")
    @Operation(summary = "存储供应商工商信息" , description = "存储供应商工商信息,已存在则更新，不存在则新增")
    public ServerResponseEntity<Void> storage(@Valid @RequestBody SupplierCompanyDTO supplierCompanyDTO) {
        Long supplierId = this.getSupplierId();
        SupplierCompanyVO supplierCompanyVO = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.WAITAUDIT.value());
        SupplierCompany supplierCompany = mapperFacade.map(supplierCompanyDTO, SupplierCompany.class);
        supplierCompany.setSupplierId(supplierId);
        supplierCompany.setStatus(AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(supplierCompanyVO)) {
            // 不存在，新增
            supplierCompanyService.save(supplierCompany, true);
        } else {
            // 已存在，更新
            supplierCompany.setSupplierCompanyId(supplierCompanyVO.getSupplierCompanyId());
            supplierCompanyService.checkInfo(supplierCompany, false);
            supplierCompanyService.update(supplierCompany);
        }
        return ServerResponseEntity.success();
    }

    @GetMapping
    @Operation(summary = "获取店铺工商信息")
    @Parameter(name = "status", description = "审核状态：1：已通过 0待审核 -1未通过" )
    public ServerResponseEntity<SupplierCompanyVO> getSupplierCompanyBySupplierId(@RequestParam(value = "status",required = false, defaultValue = "1") Integer status) {
        Long supplierId = this.getSupplierId();
        Integer isPassShop = AuthUserContext.get().getIsPassShop();
        if (Objects.equals(isPassShop, StatusEnum.DISABLE.value())) {
            status = AuditStatus.WAITAUDIT.value();
       }
        return ServerResponseEntity.success(supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, status));
    }

    private Long getSupplierId() {
        Long supplierId = AuthUserContext.get().getTenantId();
        if (Objects.equals(supplierId, Constant.DEFAULT_SUPPLIER_ID)) {
            throw new LuckException("供应商不存在");
        }
        return supplierId;
    }

}
